/**************************************
*                                     *
*   Jeff Molofee's Basecode Example   *
*          nehe.gamedev.net           *
*                2001                 *
*                                     *
*    All Code / Tutorial Commenting   *
*       by Jeff Molofee ( NeHe )      *
*                                     *
**************************************/

#include "GameEngine/Lesson36.h"

// User Defined Variables
float		angle;												// Used To Rotate The Helix
float		vertexes[4][3];										// Holds Float Info For 4 Sets Of Vertices
float		normal[3];											// An Array To Store The Normal Data
GLuint		BlurTexture;										// An Unsigned Int To Store The Texture Number

#define STRETCH 5
const GLfloat ScreenRect[] = {
	0-STRETCH, 512-(512-SCREEN_HEIGHT)+(STRETCH*2),
	0-STRETCH, -(512-SCREEN_HEIGHT)-(STRETCH*2),
	512+(STRETCH*2), 512-(512-SCREEN_HEIGHT)+(STRETCH*2),
	512+(STRETCH*2), -(512-SCREEN_HEIGHT)-STRETCH
};

const GLfloat ScreenRectTexCoord[] = {
	0.0f,0.0f,
	0.0f,1.0f,
	1.0f,0.0f,
	1.0f,1.0f,
};

void ViewOrtho(){												// Set Up An Ortho View
	::glDisable(GL_DEPTH_TEST);
	::glDisable(GL_CULL_FACE);
	::glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
	::glMatrixMode(GL_PROJECTION);								// Select Projection
	::glLoadIdentity();											// Reset The Matrix
	::glOrthof( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -10, 1000);							// Select Ortho Mode (SCREEN_WIDTHxSCREEN_HEIGHT)
	::glMatrixMode(GL_MODELVIEW);									// Select Modelview Matrix
	::glLoadIdentity();											// Reset The Matrix
}

GLuint EmptyTexture()											// Create An Empty Texture
{
	GLuint txtnumber;											// Texture ID
	unsigned int* data;											// Stored Data

	// Create Storage Space For Texture Data (128x128x4)
	data = (unsigned int*)new GLuint[((512 * 512)* 3 * sizeof(unsigned int))];
	//ZeroMemory(data,((128 * 128)* 3 * sizeof(unsigned int)));	// Clear Storage Memory

	glGenTextures(1, &txtnumber);								// Create 1 Texture
	glBindTexture(GL_TEXTURE_2D, txtnumber);					// Bind The Texture
	glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, data);						// Build Texture Using Information In data
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

	delete [] data;												// Release data

	return txtnumber;											// Return The Texture ID
}
void RenderToTexture(){											// Renders To A Texture

	//glViewport(0,0,128,128);									// Set Our Viewport (Match Texture Size)
	//glFlush();

	glBindTexture(GL_TEXTURE_2D,BlurTexture);					// Bind To The Blur Texture

	// Copy Our ViewPort To The Blur Texture (From 0,0 To 128,128... No Border)
	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 512, 512, 0);

	//glViewport(0 , 0,SCREEN_WIDTH ,SCREEN_HEIGHT);				// Set Viewport (0,0 to SCREEN_WIDTHxSCREEN_HEIGHT)
}

void DrawBlur(int times, float inc){								// Draw The Blurred Image

	RenderToTexture();
	float spost = 0.0f;											// Starting Texture Coordinate Offset
	float alphainc = 0.9f / times;								// Fade Speed For Alpha Blending
	float alpha = 0.2f;											// Starting Alpha Value

	glEnable(GL_TEXTURE_2D);									// Enable 2D Texture Mapping
	glBlendFunc(GL_SRC_ALPHA,GL_SRC_COLOR);							// Set Blending Mode
	glEnable(GL_BLEND);											// Enable Blending
	glBindTexture(GL_TEXTURE_2D,BlurTexture);					// Bind To The Blur Texture
	ViewOrtho();												// Switch To An Ortho View

	alphainc = alpha / times;									// alphainc=0.2f / Times To Render Blur

/*	
	glBegin(GL_QUADS);											// Begin Drawing Quads
		for (int num = 0;num < times;num++)						// Number Of Times To Render Blur
		{
			glColor4f(1.0f, 1.0f, 1.0f, alpha);					// Set The Alpha Value (Starts At 0.2)
			glTexCoord2f(0+spost,1-spost);						// Texture Coordinate	( 0, 1 )
			glVertex2f(0,0);									// First Vertex		(   0,   0 )

			glTexCoord2f(0+spost,0+spost);						// Texture Coordinate	( 0, 0 )
			glVertex2f(0,480);									// Second Vertex	(   0, 480 )

			glTexCoord2f(1-spost,0+spost);						// Texture Coordinate	( 1, 0 )
			glVertex2f(640,480);								// Third Vertex		( 640, 480 )

			glTexCoord2f(1-spost,1-spost);						// Texture Coordinate	( 1, 1 )
			glVertex2f(640,0);									// Fourth Vertex	( 640,   0 )

			spost += inc;										// Gradually Increase spost (Zooming Closer To Texture Center)
			alpha = alpha - alphainc;							// Gradually Decrease alpha (Gradually Fading Image Out)
		}
	glEnd();													// Done Drawing Quads
*/
	
	::glColor4f(1.0f,1.0f,1.0f,0.7f);
	::glEnableClientState(GL_VERTEX_ARRAY);
	::glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	::glVertexPointer(2,GL_FLOAT,0,&ScreenRect);
	::glTexCoordPointer(2,GL_FLOAT,0,&ScreenRectTexCoord);
	::glDrawArrays(GL_TRIANGLE_STRIP,0,4);

	GameEngine::getCamera()->Projection3D();
	//::glViewport(0,0,SCREEN_WIDTH/2,SCREEN_HEIGHT/2);
//	ViewPerspective();											// Switch To A Perspective View

	glEnable(GL_DEPTH_TEST);									// Enable Depth Testing
	glDisable(GL_TEXTURE_2D);									// Disable 2D Texture Mapping
	glDisable(GL_BLEND);										// Disable Blending
	glBindTexture(GL_TEXTURE_2D,0);								// Unbind The Blur Texture
}

bool initBlur(){					// Any GL Init Code & User Initialiazation Goes Here
	
	// Start Of User Initialization
	angle		= 0.0f;											// Set Starting Angle To Zero

	BlurTexture = EmptyTexture();								// Create Our Empty Texture
	//::glViewport(0,0,SCREEN_WIDTH/2,SCREEN_HEIGHT/2);

	return true;												// Return TRUE (Initialization Successful)
}

void Deinitialize (void)										// Any User DeInitialization Goes Here
{
	glDeleteTextures(1,&BlurTexture);							// Delete The Blur Texture
}

void Draw (void){												// Draw The Scene
	glClearColor(0.0f, 0.0f, 0.0f, 0.5);						// Set The Clear Color To Black
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear Screen And Depth Buffer
	glLoadIdentity();											// Reset The View	
	RenderToTexture();											// Render To A Texture

	DrawBlur(25,0.02f);											// Draw The Blur Effect
}
